package kitrobot.com.bed.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import kitrobot.com.bed.Service.RegisteService;
import kitrobot.com.wechat_bottom_navigation.R;

public class RegisteActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String[] m={"医疗人员","普通用户"};
    private TextView view ;
    private EditText et_rtel;
    private EditText et_rname;
    private EditText et_rpass;
    private EditText et_cfpass;
    private Spinner spinner;
    public Integer type;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.registe);
        view = (TextView) findViewById(R.id.tv_rsf);
        spinner = (Spinner) findViewById(R.id.sp_rsf);
        adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,m);
        et_rtel= (EditText) findViewById(R.id.et_rtel);
        et_rname= (EditText) findViewById(R.id.et_rname);
        et_rpass= (EditText) findViewById(R.id.et_rpass);
        et_cfpass= (EditText) findViewById(R.id.et_cfpass);
        //设置下拉列表的风格
        adapter.setDropDownViewResource(R.layout.dropdown_style);

        //将adapter 添加到spinner中
        spinner.setAdapter(adapter);

        //添加事件Spinner事件监听
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
        //设置默认值
        spinner.setVisibility(View.VISIBLE);


    }
    class SpinnerSelectedListener implements OnItemSelectedListener{

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {
            view.setText("您的身份是："+m[arg2]);

            if (m[arg2].equals("医疗人员")){
                type=0;
            }
            if (m[arg2].equals("普通用户")){
                type=1;
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }

    public void Registeto(View view) {
       final String phone=et_rtel.getText().toString().trim();
       final String name=et_rname.getText().toString().trim();
       final String password=et_rpass.getText().toString().trim();
       final String password2=et_cfpass.getText().toString().trim();
        String a= String.valueOf(isMobileNO( phone));
       if (phone.length()==0){
           Toast.makeText(RegisteActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
       }
       else if(a.equals("false")||phone.length()<11){
           Toast.makeText(RegisteActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
       }
       else if (name.length()==0){
           Toast.makeText(RegisteActivity.this,"用户名不能为空！",Toast.LENGTH_LONG).show();
        }
       else if (name.length()<2){
           Toast.makeText(RegisteActivity.this,"用户名不能小于2位！",Toast.LENGTH_LONG).show();
       }
       else if(password.length()==0){
           Toast.makeText(RegisteActivity.this,"密码不能为空！",Toast.LENGTH_LONG).show();
       }
       else if(password.length()<6){
           Toast.makeText(RegisteActivity.this,"密码不能小于6位！",Toast.LENGTH_LONG).show();
       }
       else if(password2.length()==0){
           Toast.makeText(RegisteActivity.this,"确认密码不能为空！",Toast.LENGTH_LONG).show();
       }
       else if(!password.equals(password2)){
           Toast.makeText(RegisteActivity.this,"两次输入密码不一致！",Toast.LENGTH_LONG).show();
       }
       else {
            new Thread(){
                public void run(){
                    final String result= RegisteService.RegisteByClientPost(phone,name,password,type);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String errno=null;
                                Toast.makeText(RegisteActivity.this,result,Toast.LENGTH_LONG).show();
                                try {
                                    //JSON字符串

                                    //根据JSON字符串 获得JSON object对象
                                    JSONObject jsonObject = new JSONObject(result);
                                    //根据key 获取相应的value
                                    errno=jsonObject.getString("errno");
                                    Toast.makeText(RegisteActivity.this,"注册成功，请登录！",Toast.LENGTH_LONG).show();
                                    if(errno.equals("0")){

                                        Intent intent=new Intent(RegisteActivity.this,LoginActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }

                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisteActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();
       }
    }
    public static boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[01256789]\\d{8}|17[0678]\\d{8}"; ;
        if (TextUtils.isEmpty(mobiles)) return false ;
        else return mobiles.matches( telRegex ) ;
    }
    public void Finish(View view)
    {finish();}
    @Override
    public void onClick(View view) {

    }

}
